package com.edu.controller;

import com.edu.dao.CommentMapper;
import com.edu.data.Comment;
import com.edu.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/comment")
public class CommentController {
    SqlSession sqlSession= MyBatisUtil.createSession();
    CommentMapper commentMapper=sqlSession.getMapper(CommentMapper.class);

    @RequestMapping("/toComment")
    private void toComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        List<Comment> books = commentMapper.findBookById(null);
        req.setAttribute("comments",books);
        req.getRequestDispatcher("../static/manage/guestbook.jsp").forward(req,resp);
    }

    @RequestMapping("/readComment")
    private void readComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, ServletException, IOException {
        List<Comment> book = commentMapper.findBookById(Long.parseLong(req.getParameter("entityId")));
        req.setAttribute("entityId",book.get(0).getEcId());
        req.setAttribute("reply", book.get(0).getEcReply());
        req.setAttribute("content", book.get(0).getEcContent());
        req.setAttribute("nickName", book.get(0).getEcNickName());
        req.setAttribute("replayTime", book.get(0).getEcReplyTime());
        req.getRequestDispatcher("../static/manage/guestbook-modify.jsp").forward(req,resp);
    }

    @RequestMapping("/delComment")
    private void delComment(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        commentMapper.deleteBook(Integer.parseInt(req.getParameter("entityId")));
        resp.sendRedirect("toComment");
    }

    @RequestMapping("/saveComment")
    private void save(HttpServletRequest req, HttpServletResponse resp) throws SQLException, IOException {
        if (!req.getParameter("entityId").isEmpty()){
            List<Comment> books = commentMapper.findBookById(Long.parseLong(req.getParameter("entityId")));
            books.get(0).setEcReply(req.getParameter("reply"));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            books.get(0).setEcReplyTime(df.format(new Date()));
            commentMapper.updateBook(books.get(0));
        }else {
            Comment comment = new Comment();
            comment.setEcNickName(req.getParameter("nickName"));
            comment.setEcContent(req.getParameter("content"));
            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
            comment.setEcCreateTime(df.format(new Date()));
            commentMapper.insert(comment);
        }
        resp.sendRedirect("toComment");
    }
}
